package com.gykfbxxh.imovie.adapter;

import android.app.Activity;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.utils.util.ViewSizeUtils;
import com.gykfbxxh.imovie.AppApplication;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.bean.config.ChannelItem;
import com.gykfbxxh.imovie.custom.view.vpindicator.IndicatorViewPager;
import com.gykfbxxh.imovie.view.main.fragment.FilmsFragment;

import java.util.List;

/**
 * Created by xuxinghai
 * On 2017/9/11
 */

public class MyIndicatorViewPagerAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {
    private Activity mActivity;
    private List<ChannelItem> userChannelList ;
    public LayoutInflater inflater;

    public MyIndicatorViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    public MyIndicatorViewPagerAdapter(FragmentManager fragmentManager,Activity mActivity, List<ChannelItem> userChannelList) {
        super(fragmentManager);
        if (this.mActivity == null) {
            inflater = LayoutInflater.from(AppApplication.getInstance());
        } else {
            inflater = LayoutInflater.from(mActivity);
        }
        this.mActivity = mActivity;
        this.userChannelList = userChannelList;
    }

    @Override
    public int getCount() {
        return userChannelList.size();
    }

    @Override
    public View getViewForTab(int position, View convertView, ViewGroup container) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.tab_top, container, false);
        }
        ChannelItem channelItem = userChannelList.get(position);
        TextView textView = (TextView) convertView;
        textView.setPadding(0,ViewSizeUtils.dp2px(mActivity,-5),0,ViewSizeUtils.dp2px(mActivity,-10));
        textView.setText(channelItem.getName());

        int witdh = getTextWidth(textView);
        int padding = ViewSizeUtils.dp2px(mActivity, 8);
        //因为wrap的布局 字体大小变化会导致textView大小变化产生抖动，这里通过设置textView宽度就避免抖动现象
        //1.3f是根据上面字体大小变化的倍数1.3f设置
        textView.setWidth((int) (witdh * 1.3f) + padding);

        return convertView;
    }

    @Override
    public Fragment getFragmentForPage(int position) {
        FilmsFragment fragment = new FilmsFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", userChannelList.get(position).getId());
        fragment.setArguments(bundle);
        fragment.flushData();
        fragment.notifyDown();
        return fragment;
    }


    @Override
    public int getItemPosition(Object object) {
        //这是ViewPager适配器的特点,有两个值 POSITION_NONE，POSITION_UNCHANGED，默认就是POSITION_UNCHANGED,
        // 表示数据没变化不用更新.notifyDataChange的时候重新调用getViewForPage
        return PagerAdapter.POSITION_UNCHANGED;
    }

    private int getTextWidth(TextView textView) {
        if (textView == null) {
            return 0;
        }
        Rect bounds = new Rect();
        String text = textView.getText().toString();
        Paint paint = textView.getPaint();
        paint.getTextBounds(text, 0, text.length(), bounds);
        int width = bounds.left + bounds.width();
        return width;
    }



}
